<!--
    This script is a hack used to support single chrome extension usage on any domain.
    This script has issues, though.
    It uses "postMessage" mechanism which fails to work if someone is using it from inside an <iframe>.
    The only solution for such cases is, use WebSockets or external servers to pass "source-ids".

    Note: If you want to host it yourself:
    1) open ScreenId.js
    2) replace following with "getScreenId.html" link:

    iframe.src = 'https://www.webrtc-experiment.com/getSourceId/'; // replace it with below line
    iframe.src = 'https://www.yourdomain.com/getScreenId.html';
-->
<script>
// https://github.com/muaz-khan/DetectRTC
var DetectRTC = {};

var screenCallback;

DetectRTC.screen = {
    chromeMediaSource: 'screen',
    getSourceId: function(callback) {
        if (!callback) throw '"callback" parameter is mandatory.';

        screenCallback = callback;
        window.postMessage('get-sourceId', '*');
    },
    getCustomSourceId: function(arr, callback) {
        if (!arr || !arr.forEach) throw '"arr" parameter is mandatory and it must be an array.';
        if (!callback) throw '"callback" parameter is mandatory.';

        screenCallback = callback;
        window.postMessage({
            'get-custom-sourceId': arr
        }, '*');
    },
    getSourceIdWithAudio: function(callback) {
        if (!callback) throw '"callback" parameter is mandatory.';

        screenCallback = callback;
        window.postMessage('audio-plus-tab', '*');
    },
    onMessageCallback: function(data) {
        // "cancel" button is clicked
        if (data == 'PermissionDeniedError') {
            DetectRTC.screen.chromeMediaSource = 'PermissionDeniedError';
            if (screenCallback) return screenCallback('PermissionDeniedError');
            else throw new Error('PermissionDeniedError');
        }

        // extension notified his presence
        if (data == 'rtcmulticonnection-extension-loaded') {
            DetectRTC.screen.chromeMediaSource = 'desktop';
        }

        // extension shared temp sourceId
        if (data.sourceId) {
            DetectRTC.screen.sourceId = data.sourceId;
            
            if (screenCallback) {
                screenCallback(DetectRTC.screen.sourceId, data.canRequestAudioTrack === true);
            }
        }
    },
    getChromeExtensionStatus: function(callback) {
        // https://chrome.google.com/webstore/detail/screen-capturing/ajhifddimkapgcifgcodmmfdlknahffk
        var extensionid = 'ajhifddimkapgcifgcodmmfdlknahffk';

        var image = document.createElement('img');
        image.src = 'chrome-extension://' + extensionid + '/icon.png';
        image.onload = function() {
            if (!DetectRTC.screen) DetectRTC.screen = {};
            if (!DetectRTC.screen.chromeMediaSource) DetectRTC.screen.chromeMediaSource = '';

            if (DetectRTC.screen.chromeMediaSource === 'desktop') {
                callback('installed-enabled');
                return;
            }

            DetectRTC.screen.chromeMediaSource = 'screen';
            window.postMessage('are-you-there', '*');
            setTimeout(function() {
                if (DetectRTC.screen.chromeMediaSource == 'screen') {
                    callback('installed-disabled');
                } else {
                    callback('installed-enabled');
                }
            }, 2000);
        };
        image.onerror = function() {
            callback('not-installed');
        };
    }
};

window.addEventListener('message', function(event) {
    if (!event.data || !(typeof event.data == 'string' || event.data.sourceId || event.data.captureSourceId || event.data.captureCustomSourceId || event.data.getChromeExtensionStatus || event.data.captureSourceIdWithAudio)) return;

    if (event.data.getChromeExtensionStatus) {
        DetectRTC.screen.getChromeExtensionStatus(function(status) {
            window.parent.postMessage({
                chromeExtensionStatus: status
            }, '*');
        });
        return;
    }

    if(event.data.captureCustomSourceId) {
        captureSourceId(null, event.data.captureCustomSourceId);
    }

    if (event.data.captureSourceId) {
        captureSourceId();
    }

    if (event.data.captureSourceIdWithAudio) {
        captureSourceId('system_audio');
    }

    DetectRTC.screen.onMessageCallback(event.data);
});

function captureSourceId(system_audio, custom_sourceId) {
    // check if desktop-capture extension installed.
    DetectRTC.screen.getChromeExtensionStatus(function(status) {
        if (status != 'installed-enabled') {
            window.parent.postMessage({
                chromeExtensionStatus: status
            }, '*');
            return;
        }

        if(!!custom_sourceId) {
            DetectRTC.screen.getCustomSourceId(custom_sourceId, function(sourceId, canRequestAudioTrack) {
                window.parent.postMessage({
                    chromeMediaSourceId: sourceId,
                    canRequestAudioTrack: !!canRequestAudioTrack
                }, '*');
            });
        }
        else if(!system_audio && !custom_sourceId) {
            DetectRTC.screen.getSourceId(function(sourceId) {
                window.parent.postMessage({
                    chromeMediaSourceId: sourceId
                }, '*');
            });
        }
        else {
            DetectRTC.screen.getSourceIdWithAudio(function(sourceId, canRequestAudioTrack) {
                window.parent.postMessage({
                    chromeMediaSourceId: sourceId,
                    canRequestAudioTrack: !!canRequestAudioTrack
                }, '*');
            });
        }
    });
}
</script>
